﻿@using Single_SignOn.ResX
@model Single_SignOn.Areas.Handheld.Models.Login

@{
    ViewBag.Title = @ResourceTools.GetResource("Resource", "titleLogin");
    Layout = "~/Areas/Handheld/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("", "", new AjaxOptions {HttpMethod = "POST"})) 
{
    @Html.ValidationSummary(true)
    <div id="content"  >
        <table style="width: 100%; text-align: center;">
            <tr>
                <td style="width: 100%; text-align: center; vertical-align: middle;">
                    <div>
                        <fieldset>
                            <legend style="width: 100%; text-align: center; vertical-align: middle;">
                                <h5>@ResourceTools.GetResource("Resource", "LoginHeader")</h5>
                            </legend>
                            <p></p>
                            <div class="center">
                                        <table style="width: 100%; text-align: center;">
                                            <tr>
                                                <td style="width: 100%;" class="center">
                                                    @Html.LabelFor(m => m.Username, new { @class = "label" })
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="width: 100%;" class="center">
                                                    @Html.TextBoxFor(m => m.Username, new { @class = "textbox" })
                                                    <div class="error">
                                                        <div class="error-username"></div>
                                                    </div>
                                                </td>
                                            </tr>
                                        </table>
                                        <table style="width: 100%; text-align: center;">
                                            <tr>
                                                <td style="width: 100%;" class="center">
                                                    @Html.LabelFor(m => m.Password, new { @class = "label" })
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="width: 100%;" class="center">
                                                    @Html.TextBoxFor(m => m.Password, new { @class = "textbox", @type = "password" })
                                                    <div class="error">
                                                        <div class="error-password"></div>
                                                    </div>
                                                </td>
                                            </tr>
                                        </table>
                            </div>
                        <div style="text-align: center;" class="error">
                            <p></p>
                            <div class="error-summary"></div>
                        </div>
                            <div class="center">
                                <table style="width: 100%; text-align: center;">
                                    <tr>
                                        <td style="width: 100%; text-align: center;">
                                          <button type="button" name="Login" id="Login" title = "@ResourceTools.GetResource("Resource", "Login")">
                                              <img src="~/Images/lock.gif" alt=" @ResourceTools.GetResource("Resource", "Login") "/>
                                              @ResourceTools.GetResource("Resource", "Login")
                                        </button>
                                      </td>
                                    </tr>
                                    <tr>
                                        <td style="width: 100%; text-align: center;">
                                            <button type="button" name="ChangePassword" id="ChangePassword" onclick=""  title = "@ResourceTools.GetResource("Resource", "ChangePassword")">
                                                <img src="~/Images/lock.gif" alt=" @ResourceTools.GetResource("Resource", "ChangePassword") "/>
                                                @ResourceTools.GetResource("Resource", "ChangePassword")
                                            </button>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </fieldset>

                    </div>
                </td>
            </tr>
        </table>
    </div>
    <!-- Json passer -->
    <div class="display">
        <a id="ChangePasswordUrl" href="@Html.AttributeEncode(Url.Action("Index", "ChangeSword"))" style="display:none;"></a>
        <script type="text/javascript">
            $(document).ready(function () {

                $('#Login').click(function () {

                    var valid = true;
                    //*
                    $(".error-summary").html("");
                    $(".error-username").html("");
                    $(".error-password").html("");
                    //*
                    if ($("#Username").val() == "") {
                        $(".error-username").html(" @ResourceTools.GetResource("Resource", "UsernameRequired") ");
                        valid = false;
                    }
                    if ($("#Password").val() == "") {
                        $(".error-password").html(" @ResourceTools.GetResource("Resource", "PasswordRequired") ");
                        valid = false;
                    }

                    if (valid == true) {
                        var oLoginDataObj = {
                            "Username": $("#Username").val(),
                            "Password": $("#Password").val(),
                            "Remember": true
                        };
                        //debugger;
                        $.ajax({
                            url: '@Url.Action("Login", "Login")',
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(oLoginDataObj),
                            success: function (data, textStatus, xhr) {
                                //At this point I would like to redirect
                                if (data.exception == "") {
                                    window.location.href = data.redirectToUrl;
                                }
                                else {
                                    //*
                                    $("#Password").val("");
                                    $(".error-username").html("");
                                    $(".error-password").html("");
                                    //*
                                    $(".error-summary").html("");
                                    $(".error-summary").html(data.exception);
                                }
                            },
                            error: function (xhr, textStatus, errorThrown) {
                                //*
                                $("#Password").val("");
                                $(".error-username").html("");
                                $(".error-password").html("");
                                //*
                                $(".error-summary").html(xhr.responseText);
                            }
                        });

                        return false;
                    }
                });

                // Clecr
                $('#ChangePassword').click(function () {
                    document.location.href = $('#ChangePasswordUrl').attr('href');
                });
            });

        </script>
    </div>
    
}